# -*- mode: python; -*-
Import('env')

installer = env['INSTALLER']

lib_name = 'keyczar'
unittest_lib_name = 'keyczar_test'

#### Optionnaly add dependancy on zlib
if env.get('ZLIB_OPTION'):
    env.Append(
        LIBS = [
            'z'
            ],
        )


#### Keyczar Library

keyczar_lib_env = env.Clone()

keyczar_lib_env.Append(
    LIBS = [
        'crypto',
        ],
    )

base_sources = [
    'base/base64w.cc',
    'base/command_line.cc',
    'base/file_util.cc',
    'base/file_path.cc',
    'base/json_reader.cc',
    'base/json_value_serializer.cc',
    'base/json_writer.cc',
    'base/logging.cc',
    'base/modp/modp_b64w.cc',
    'base/ref_counted.cc',
    'base/stl_util-inl.cc',
    'base/string_escape.cc',
    'base/string_util.cc',
    'base/values.cc',
    'base/zlib.cc',
    ]

base_posix_sources = [
    'base/file_util_posix.cc',
    ]

base_linux_sources = [
    ]

base_mac_sources = [
    ]

base_bsd_sources = [
    ]

base_win_sources = [
    'base/file_util_win.cc',
    ]

keyczar_sources = [
    'aes_key.cc',
    'cipher_mode.cc',
    'crypto_factory.cc',
    'dsa_private_key.cc',
    'dsa_public_key.cc',
    'ecdsa_impl.cc',
    'ecdsa_private_key.cc',
    'ecdsa_public_key.cc',
    'hmac_impl.cc',
    'hmac_key.cc',
    'key.cc',
    'key_purpose.cc',
    'keyczar.cc',
    'keyset.cc',
    'keyset_metadata.cc',
    'key_status.cc',
    'key_type.cc',
    'key_util.cc',
    'message_digest_impl.cc',
    'pbe_impl.cc',
    'private_key.cc',
    'public_key.cc',
    'rsa_private_key.cc',
    'rsa_public_key.cc',
    'secret_key.cc',
    'session.cc',
    'util.cc',
    ]

keyczar_tool_sources = [
    'keyczar_tool/keyczar_tool.cc',
]

openssl_sources = [
    'openssl/aes.cc',
    'openssl/dsa.cc',
    'openssl/ecdsa.cc',
    'openssl/hmac.cc',
    'openssl/message_digest.cc',
    'openssl/pbe.cc',
    'openssl/rand.cc',
    'openssl/rsa.cc',
    'openssl/util.cc',
    ]

rw_sources = [
    'rw/keyset_encrypted_file_reader.cc',
    'rw/keyset_encrypted_file_writer.cc',
    'rw/keyset_file_reader.cc',
    'rw/keyset_file_writer.cc',
    'rw/keyset_reader.cc',
    'rw/keyset_writer.cc',
]

if env.Bit('linux'):
    base_sources.extend(base_posix_sources)
    base_sources.extend(base_linux_sources)

if env.Bit('mac'):
    base_sources.extend(base_posix_sources)
    base_sources.extend(base_mac_sources)

if env.Bit('bsd'):
    base_sources.extend(base_posix_sources)
    base_sources.extend(base_bsd_sources)

#if env.Bit('win'):
#    base_sources.extend(base_win_sources)

# Build the library
lib_sources = base_sources + keyczar_sources + openssl_sources + \
              rw_sources + keyczar_tool_sources
out_lib = keyczar_lib_env.ComponentLibrary(lib_name, lib_sources,
                                           COMPONENT_STATIC=False)

# Install the library components
installer.AddLibrary(out_lib[0])
installer.AddHeaders(keyczar_lib_env.get('KEYCZAR_SRC_DIR'), "*.h",
                     lib_name, True)


#### keyczart

keyczart_env = env.Clone()

keyczart_env.Append(
    LIBS = [
        lib_name,
	'crypto',
        ],
    )

keyzart_sources = [
    'keyczar_tool/keyczart.cc',
    ]

out_program = keyczart_env.ComponentProgram('keyczart', keyzart_sources)
installer.AddProgram(out_program)

#### interop
interop_env = env.Clone()

interop_env.Append(
    LIBS = [
        lib_name,
        'crypto',
        ],
    )

interop_sources = [
    'interop/interop_test.cc',
    'interop/operation.cc'
    ]

interop_env.ComponentProgram('interop_test', interop_sources)


#### testdata_gen*

testdata_gen_env = env.Clone()

testdata_gen_env.Append(
    LIBS = [
        lib_name,
	'crypto',
        ],
    )

testdata_gen_sources = [
    'keyczar_tool/testdata_gen.cc',
    ]

testdata_gen_env.ComponentProgram('testdata_gen', testdata_gen_sources)


#### Swig interface for Python

keyczar_swig_env = env.Clone()

import distutils.sysconfig

if env.Bit('mac'):
    keyczar_swig_env.Append(
        FRAMEWORKS = [
            'Python',
            ],
        )

keyczar_swig_env.Append(
    LIBS = [
        lib_name,
	'crypto',
        ],
    SWIGFLAGS = ['-c++', '-python'],
    CPPPATH = [distutils.sysconfig.get_python_inc()],
    )

keyczar_swig_env.Replace(
    SHLIBPREFIX = ""
    )

if env.Bit('mac'):
    keyczar_swig_env.Replace(
        SHLIBSUFFIX = ".so",
        )

keyczar_swig_sources = [
    'keyczar.i',
    ]

if env.get('SWIG_OPTION'):
    prefix = '_'
    out = keyczar_swig_env.ComponentLibrary(prefix + lib_name,
                                            keyczar_swig_sources,
                                            COMPONENT_STATIC=False)
    installer.AddPythonModule(out[0], lib_name)



## Lib Unittests

lib_unittests_env = env.Clone()

if env.Bit('mac'):
    lib_unittests_env.Append(
        FRAMEWORKS = [
            'Foundation',
            ],
        )

lib_unittests_env.Append(
    CPPPATH = [
        ],
    LIBS = [
        lib_name,
        ],
    )

unittests_sources = [
    'base_test/base_paths.cc',
    'base_test/path_service.cc',
    ]

unittests_posix_sources = [
    ]

unittests_linux_sources = [
    'base_test/base_paths_linux.cc',
    ]

unittests_bsd_sources = [
    'base_test/base_paths_bsd.cc',
    ]

unittests_mac_sources = [
    'base_test/base_paths_mac.mm',
    ]

unittests_win_sources = [
    'base_test/base_paths_win.cc',
    ]

if env.Bit('linux'):
    unittests_sources.extend(unittests_posix_sources)
    unittests_sources.extend(unittests_linux_sources)

if env.Bit('mac'):
    unittests_sources.extend(unittests_posix_sources)
    unittests_sources.extend(unittests_mac_sources)

if env.Bit('bsd'):
    unittests_sources.extend(unittests_posix_sources)
    unittests_sources.extend(unittests_bsd_sources)

#if env.Bit('win'):
#    unittests_sources.extend(unittests_win_sources)

lib_unittests_out = lib_unittests_env.ComponentLibrary(unittest_lib_name,
                                                       unittests_sources,
                                                       COMPONENT_STATIC=False)


## Unittests Programs

unittests_env = env.Clone()

unittests_env.Append(
    CPPPATH = [
        '../testing/gtest',
        '../testing/gtest/include',
        ],
    LIBS = [
        lib_name,
        unittest_lib_name,
        'gtest',
        'crypto',
        ],
    )

base_unittests = [
    'base/base64w_unittest.cc',
    'base/command_line_unittest.cc',
    'base/file_path_unittest.cc',
    'base/file_util_unittest.cc',
    'base/json_reader_unittest.cc',
    'base/json_value_serializer_unittest.cc',
    'base/json_writer_unittest.cc',
    'base/ref_counted_unittest.cc',
    'base/run_all_unittests.cc',
    'base/scoped_ptr_unittest.cc',
    'base/string_escape_unittest.cc',
    'base/values_unittest.cc',
    'base/zlib_unittest.cc',
    ]

base_posix_unittests = [
    ]

base_linux_unittests = [
    ]

base_mac_unittests = [
    ]

base_bsd_unittests = [
    ]

base_win_unittests = [
    ]

if env.Bit('linux'):
    base_unittests.extend(base_posix_unittests)
    base_unittests.extend(base_linux_unittests)

if env.Bit('mac'):
    base_unittests.extend(base_posix_unittests)
    base_unittests.extend(base_mac_unittests)

if env.Bit('bsd'):
    base_unittests.extend(base_posix_unittests)
    base_unittests.extend(base_bsd_unittests)

#if env.Bit('win'):
#    base_unittests.extend(base_win_unittests)

keyczar_unittests = [
    'aes_key_unittest.cc',
    'dsa_key_unittest.cc',
    'ecdsa_key_unittest.cc',
    'hmac_key_unittest.cc',
    'keyczar_test.cc',
    'keyczar_unittest.cc',
    'keyset_metadata_unittest.cc',
    'keyset_unittest.cc',
    'rsa_key_unittest.cc',
    'run_all_unittests.cc',
    'session_unittest.cc',
    ]

openssl_unittests = [
    'openssl/aes_unittest.cc',
    'openssl/dsa_unittest.cc',
    'openssl/ecdsa_unittest.cc',
    'openssl/hmac_unittest.cc',
    'openssl/message_digest_unittest.cc',
    'openssl/pbe_unittest.cc',
    'openssl/rsa_unittest.cc',
    ]

rw_unittests = [
    'rw/keyset_reader_unittest.cc',
    ]

unittests_env.ComponentTestProgram('base_unittests', base_unittests)
unittests_env.ComponentTestProgram('keyczar_unittests', keyczar_unittests)
unittests_env.ComponentTestProgram('openssl_unittests', openssl_unittests)
unittests_env.ComponentTestProgram('rw_unittests', rw_unittests)
